/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Jul 17, 2012  8:48:56 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.business.process.commons
 * File Name : MaterialThread.java
 * TODO
 */
package apps.promis.in.material;

import java.util.ArrayList;
import java.util.List;
import jpa.entitys.synchro.staging.MmMatmasInb;
import jpa.service.synchro.staging.MmMatmasInbServices;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import apps.sessionFactory;
import apps.promis.in.common.CostArticleAction;
import apps.promis.in.common.CostArticleGroupAction;
import apps.promis.in.common.CostArticleSubGroupAction;
import apps.promis.in.common.CostProjectArticleAction;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:43:11 PM 
 * CreateBy=Chatree Siangklom
 * 
 * MaterialThread
 * TODO
 */
public class MaterialThread  extends Thread{
	private static final Logger log = Logger.getLogger(MaterialThread.class);

	private MmMatmasInb mmMatmasInb = null;
	public  MaterialThread(MmMatmasInb mmMatmasInb){
		this.mmMatmasInb = mmMatmasInb;
	}
	
	/**
	 * (Javadoc)
	 * @see java.lang.Thread#run()
	 * Date =Nov 13, 2013 : 2:43:14 PM 
	 * CreateBy=Chatree Siangklom
	 * TODO
	 */
	public void run()
    {
    	setName(this.mmMatmasInb.getArticleCode());
    	MmMatmasInbServices mmMatmasInbServ = null;
        try{
        	
        	mmMatmasInbServ = new MmMatmasInbServices(sessionFactory.CreateEmSynchro());
        	
			log.info(" mmMatmasInb.getInstanceNo : " + mmMatmasInb.getInstanceNo());
			log.info(" mmMatmasInb.getArticleCode : " + mmMatmasInb.getArticleCode());
			log.info("PromisStagingId : " + mmMatmasInb.getPromisStagingId() + " getArticleCode : " + mmMatmasInb.getArticleCode() + " getArtiGrpCode : " + mmMatmasInb.getArtiGrpCode() + " getArtiSubGrpCode : " + mmMatmasInb.getArtiSubGrpCode());
			
			//Group 
			CostArticleGroupAction costArtiGroupSrv = new CostArticleGroupAction();
			costArtiGroupSrv.SaveOrUpdateArticleGroup(mmMatmasInb);
			
			//Sub Group
			CostArticleSubGroupAction costArticleSubGroup = new CostArticleSubGroupAction();
			costArticleSubGroup.SaveOrUpdateArticleSubGroup(mmMatmasInb);
			
			//Article Master
			CostArticleAction costArticleAction = new CostArticleAction();
			costArticleAction.SaveOrUpdateCostArticle(mmMatmasInb);
			
			//Project Article
			CostProjectArticleAction  costProjectArticleAction = new CostProjectArticleAction();
			costProjectArticleAction.SaveOrUpdateCostArticle(mmMatmasInb);
			
			mmMatmasInb.setIssync("Y");
			mmMatmasInb.setMsgCode("S");
			mmMatmasInb.setMsgDesc("Success");
			mmMatmasInb.setMsgActResult(null);
			mmMatmasInbServ.Updated(mmMatmasInb);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			
			mmMatmasInb.setIssync("N");
			mmMatmasInb.setMsgCode("E");
			mmMatmasInb.setMsgDesc("Error");
			mmMatmasInb.setMsgActResult(e.getMessage());
			try {
				mmMatmasInbServ.Updated(mmMatmasInb);
			} catch (Exception e1) {
				log.error(e1.getMessage());
				System.err.println(e1.getMessage());
			}
		}
    }
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:43:20 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 */
	public void runn(){
		try{	
			MmMatmasInbServices mmMatmasInbServ = new MmMatmasInbServices(sessionFactory.CreateEmSynchro());
			List<MmMatmasInb> lst = new ArrayList<MmMatmasInb>();
			try{
				
				lst =  mmMatmasInbServ.findAllIsSync("N");
				
				//lst = new ArrayList<MmMatmasInb>();
				//lst.add(mmMatmasInbServ.findById("10000000000000066599"));
				
				if(!CommonService.IsNull(lst))
				{
					for(MmMatmasInb mmMatmasInb : lst)
					//MmMatmasInb mmMatmasInb = mmMatmasInbServ.findById("10000000000000039067");
					{	
						if(mmMatmasInb != null){
							new MaterialThread(mmMatmasInb).start();
						}
					}
				}
			}catch(Exception e)
			{
				log.error(e.getMessage());
				System.err.println(e.getMessage());
			}
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
}
